package com.zx._07_数据库.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC {

    // 连库驱动类
//	private final static String DRIVER_CLASS = "com.mysql.jdbc.Driver";// oracle.jdbc.driver.OracleDriver
    private final static String DRIVER_CLASS = "com.mysql.cj.jdbc.Driver";// oracle.jdbc.driver.OracleDriver
    // 数据库连接字符串
//	private final static String URL = "jdbc:mysql://localhost:3306/ds0?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=Asia/Shanghai";// jdbc:oracle:thin:@localhost:1521:orcl
    private final static String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";// jdbc:oracle:thin:@localhost:1521:orcl
    // 数据库用户名
    private final static String USER = "root";
    // 数据库登录密码
    private final static String PWD = "root";

    static {
        try {
            Class.forName(DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public static Connection getConnection() {
        try {
            return DriverManager.getConnection(URL, USER, PWD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     * 执行Java程序
     * java -Xms100M -Xmx200M HelloWorld
     * 死锁: ThreadDump
     * win: Ctrl+Break
     * linux: kill -3 pid
     */
    public static void release(Connection conn, Statement st, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                rs = null;// ????通过GC 回收
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                st = null;// ????通过GC 回收
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                conn = null;// ????通过GC 回收
            }
        }
    }

    public static void main(String[] args) throws Exception {
        select();
    }

    public static void insert() throws Exception {
        Connection con = JDBC.getConnection();
        String sql = "INSERT INTO t_user_0 (NAME) VALUES (?)";
        PreparedStatement ps = con.prepareStatement(sql);

        for (int i = 0; i < 10000; i++) {
            ps.setInt(1, i + 1);
            ps.addBatch();//添加批！这一组参数就保存到集合中了。
        }

        long start = System.currentTimeMillis();
        ps.executeBatch();//执行批！
        long end = System.currentTimeMillis();

        System.out.println(end - start);//412764, 301
    }

    public static void select() throws Exception {
        Connection con = JDBC.getConnection();
        String sql = "SELECT NAME FROM t_user WHERE id = 1";
        PreparedStatement ps = con.prepareStatement(sql);

        ResultSet resultSet = ps.executeQuery();

        if (resultSet.next()) {
            String name = resultSet.getString("name");
            String pName = "Jone";
            String pName2 = "Jone";
            System.out.println(pName.equals(name));// true
            System.out.println(pName == name);      // false
            System.out.println(pName == pName2);    // true
        }

    }
}
